import axios, { AxiosResponse } from 'axios'

//设置 base url。
// axios.defaults.baseURL = "/api";

const whiteList = []

//配置请求拦截器
axios.interceptors.request.use((config) => {
  //为当前请求配置 token 认证字段
  console.log('url: ', config.url)
  if ('/ucenter/auth' != config.url) {
    const token = localStorage.getItem('TOKEN')
    if (token) {
      config.headers.token = token
    }
  }
  return config
})

//配置响应拦截器
axios.interceptors.response.use(
  function (response: AxiosResponse<any>) {
    const res = response.data

    //处理文件
    if (
      response.config.url?.endsWith(
        '/recipeservice/materialConfig/export/excel'
      )
    ) {
      return res
    }

    console.log('res2', res)
    //内码错误处理
    if (res.code != 200) {
      console.log('error', res)
      throw Error(res.msg)
    }
    console.log('right1', res.data)
    return res.data
  },
  (error) => {
    throw Error(error.error || error)
    console.log('error: ', error)
  }
)
